package com.objecteffects.ilmp.web.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.objecteffects.ilmp.util.logger.InjectLogger;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.validation.Validate;

/**
 * @author Rusty Wright
 */
public class TumblrResponseUrlActionBean extends BaseActionBean {
    @InjectLogger
    private Logger log;

    private final List<Integer> batchSizes = Arrays.asList(Integer.valueOf(20),
                                                           Integer.valueOf(30),
                                                           Integer.valueOf(40),
                                                           Integer.valueOf(50));

    @Validate(minvalue = 0, required = true)
    private int startingOffset;

    @Validate(minvalue = 0, required = true)
    private int batchSize;

    private final List<String> methods = new ArrayList<>();

    private String method;

    @DontValidate
    @DefaultHandler
    public Resolution startHandler() {
        for (final TumblrMethod tumblrMethod : TumblrMethod.values()) {
            if (tumblrMethod.toActionBean() != null) {
                this.methods.add(tumblrMethod.toString());
            }
        }

        Collections.sort(this.methods);

        return new ForwardResolution(IlmpView.TUMBLR_RESPONSE_URL);
    }

    public Resolution proceed() {
        /* hammer in POSTS, no choice for now */
        // final String method = TumblrMethod.POSTS.toString();

        this.log.debug("starting offset: {}",
                       Integer.valueOf(this.startingOffset));
        this.log.debug("batch size: {}", Integer.valueOf(this.batchSize));
        this.log.debug("method: {}", this.method);

        if (StringUtils.isBlank(this.method)) {
            this.getContext().getMessages()
            .add(new SimpleMessage("Select a method."));

            return new RedirectResolution(this.getClass());
        }

        this.getContext().setStartingOffset(Integer
                                            .valueOf(this.startingOffset));

        this.getContext().setBatchSize(Integer.valueOf(this.batchSize));

        final Class<? extends BaseActionBean> actionBean =
            TumblrMethod.fromString(this.method).toActionBean();

        if (actionBean == null) {
            this.log
                .error("got null from TumblrMethod.toActionBean; method: {}",
                       this.method);

            return new ForwardResolution(IlmpView.ERROR);
        }

        return new RedirectResolution(actionBean);
    }

    public List<Integer> getBatchSizes() {
        return this.batchSizes;
    }

    public int getStartingOffset() {
        return this.startingOffset;
    }

    public void setStartingOffset(final int _startingOffset) {
        this.startingOffset = _startingOffset;
    }

    /**
     * @return the batchSize
     */
    public int getBatchSize() {
        return this.batchSize;
    }

    /**
     * @param batchSize1
     *        the batchSize to set
     */
    public void setBatchSize(final int _batchSize) {
        this.batchSize = _batchSize;
    }

    public List<String> getMethods() {
        return this.methods;
    }

    public String getMethod() {
        return this.method;
    }

    public void setMethod(final String _method) {
        this.method = _method;
    }
}
